|
ContentsLanguage basicsSCL is a functional programming language that is based on lambda calculus. Lambda calculus is a minimal but still Turing complete language with three constructions:
These are also the most central elements of SCL and most of the other constructions are only syntactic sugar on top of lambda calculus. SCL syntax is very close to the syntax of Haskell programming language and many tutorials on Haskell apply also to SCL. The main difference between the languages is the evaluation strategy: Haskell evaluates terms lazily, SCL strictly. Unlike Haskell, SCL allows side-effects during function application, but controls them with effect typing. In this respects, it behaves similarly to ML or OCaml. SCL inherits the following philosophy from Haskell and other programming languages in functional paradigm:
This section gives a walkthrough for the most importatant constructs of SCL language. LiteralsSCL supports the following types of constant expressions: Integers
Floating point numbers
String literals SCL supports single-line strings (enclosed in quotes)
and multi-line strings (enclosed in triple quotes)
Single-line strings may contain escaped characters ( Character literals
Boolean constants are written as IdentifiersVariable and constant names start with lower case letter followed by letters, digits, The following names are reserved and cannot be used as identifiers:
Identifiers starting with upper case letter are constructors and they can be defined only
together with the new data types. Examples: Function applicationsA function is applied by writing the function and its parameters consecutively.
A parameter needs to be closed in parenthesis if it is not a variable, literal, or an expression starting
with
For example
is evaluated as
because the function application has higher precedence than any binary operator. Parentheses are also needed around negation
because otherwise the expression is tried to compile as
causing a compilation error because Binary operatorsBinary operators are normal functions that are just written between their parameters:
Each binary operator can be converted into ordinary function by putting parentheses around it
Similarly an ordinary function can be converted into binary operator by putting backticks (`) around it.
Binary operators have precedences that determines how multiple consecutive binary operators are compiled. For example
is evaluated as
Variable definitionsVariables are defined by syntax
for example
Defined variable values are available in the consecutive expressions:
Function definitionsFunctions are defined by writing a function application in the left-hand side of the equation:
Defined functions are available in the consecutive expressions:
Conditional expressionsConditional expressions are written in the form:
The
RecursionA function definition can refer to itself. For example, the Euclidean algorithm for computing the greatest common divisor of two integers can be written as:
Local definitionsVariable and function definitions can be written as a part of larger expression. There are three language constructs for this purpose:
They are quite similar and it is usually just a stylistic choice which one to use. If you are only defining local variables that are needed in a subexpression
and their computation does not have side-effects (or has only reading effects),
the most natural choice is
Let-expressions can be freely embedded in other expressions:
Another construction allowing local variable bindings is
The final binding construction is
Functions taking other functions as parametersIn SCL, functions are ordinary values that can be stored to variables and manipulated. For example, writing only the function name to the console produces:
We can define for example a numerical derivative operation
Now,
Anonymous function definitionsFunctional style of programming favors lots of small functions. Giving a name for all of them becomes quickly tedious and therefore functions can be defined also anonymously. For example
defines a function that increases its parameter by one. Thus
returns Assuming that the function
Partial function applicationIt is possible to give a function less parameters that it accepts:
Such a partial application creates a function that expects the missing parameters:
It is possible to partially apply also binary operators giving only one of its parameters. Such an application must always be enclosed in parentheses
Pattern matchingThe constructors are special kind of values and functions
that can be used in the left-hand side of the function and
value definitions. Most common constructors are
the tuple constructors
Other constructors are used like functions, but the name
of the constructor is capitalized, for example
This example demonstrates also some other features. A function
can be defined with multiple equations and the first matching
equation is used. Also, if some parameter value is not used,
it may be replaced by Indentation |